/*
Copyright 2007 Julian Asamer

This file is part of Fractalicious.

    Fractalicious is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Fractalicious is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Fractalicious.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIPushButton.h>
#import <UIKit/UIThreePartButton.h>
#import <UIKit/UINavigationBar.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIView-Hierarchy.h>
#import <UIKit/UIHardware.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UIAnimation.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIFrameAnimation.h>
#import "DB.h"
#import "FractalController.h"
#import "FractaliciousApplication.h"
#import "FOptions.h"
#import "FAlgorithm.h"
#import "FRecalcTypes.h"

static FractalController * sharedInstance;

@implementation FractalController

+(FractalController *)sharedInstance {
	if (!sharedInstance)
		sharedInstance = [[FractalController alloc] init];
	return sharedInstance;
}

- (id)init {
	[super init];
	//NSLog(@"WTF!!!");
	//data = [[DrawingData alloc] init];
	return self;
}

- (void)calculationFinished:(DrawableFractal *)df {
	data = df;
	if (fractalView!=nil) {
		//NSLog(@"Finished calculation!");
		if (df==nil) {
			NSLog(@"But df is nil...");
		}
		[fractalView setFractalData: df];
		NSLog(@"calculated.");
		
		[fractalView setNeedsDisplay];
	}
}

- (void)viewChanged:(NSAffineTransform *)t {
	NSLog(@"ViewChanged");
	[fractalView setNeedsDisplay];
}

- (void)toggleBars {
	if ([bottomNav alpha]==1.0f) {
		[self hideBars];
	} else {
		[self showBars];
	}
}
- (void)showBars {
	if (![bottomNav alpha]==1.0f) {
		[fractalFrame addSubview: bottomNav];
		[UIView beginAnimations:nil];    
		[UIView setAnimationDuration:0.4];
		
		[bottomNav setAlpha: 1.0f];
		[UIView endAnimations];
	}
}
- (void)hideBars {
	if ([bottomNav alpha]==1.0f) {
		[UIView beginAnimations:nil]; 
		[UIView setAnimationDelegate: self];
		[UIView setAnimationDidStopSelector: @selector(removeNavBar)];  
		[UIView setAnimationDuration:0.4];
		[bottomNav setAlpha: 0.0f];
		[UIView endAnimations];
	}
}

- (void) removeNavBar {
	[bottomNav removeFromSuperview];
}

- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button {
	switch (button) 
	{
		case 0: {
			NSLog(@"ShowOptions2");
			[optionsTable show];
			[bottomNav setDelegate: optionsTable];
			UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"Options"];
			[bottomNav pushNavigationItem: navItem];
			[bottomNav showButtonsWithLeftTitle: @"Done" rightTitle: nil leftBack: YES];
			break;
		} 
		case 1:{
			NSLog(@"MoveBack");
			if (fractalView != nil) [fractalView reset];
			[[FractaliciousApplication sharedInstance] moveBackFromFractal];
			break;
		}
	}
}

- (void)backFromFOptions {

	[bottomNav popNavigationItem];
	[bottomNav showButtonsWithLeftTitle: @"Close" rightTitle: @"More" leftBack: YES];

	[bottomNav setDelegate: self];
}

- (void)setFractal:(FractalDescription *)des {
	[bottomNav popNavigationItem];

	UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: [des getName]];
	[bottomNav pushNavigationItem: navItem];
	if (c == nil) {
		c = [[FAlgorithm alloc] init];
		[c setDelegate: self];
	}

	fractal = [[[des getClass] alloc] init];
	[fractal setDelegate: self];
	[c setDataSource: fractal];
	[c calculateFractal: Full];
	NSLog(@"Added Fractal!");
}

- (NSMutableArray *) getDragables {
	return [fractal getDragables];
}

- (void)startRecalculation: (RecalcType) type {
	//if (type==Drawonly) 
		//[self calculationFinished: data];
	//else 
	NSLog(@"Started.");
	[c calculateFractal: Fast];
}

- (UINavigationBar *)getNavigation {
	return bottomNav;
}

- (UIView *)getView {
	if (fractalView==nil) {
		fractalFrame = [[UIView alloc] initWithFrame: CGRectMake(0.0f, 0.0f,
				320.0f, 480.0f)];
		
		optionsTable = [[FOptions alloc] init];
		[optionsTable setDelegate: self];
		
		fractalView = [[FView alloc] init];
		[fractalView setDelegate: self];
		[fractalView addOptionsToFOptions: optionsTable];

		bottomNav = [[UINavigationBar alloc] initWithFrame: CGRectMake(
	        0.0f, 432.0f, 320.0f, 48.0f)];
	    [bottomNav setBarStyle: 1];
		UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"Spiral"];
		[bottomNav showButtonsWithLeftTitle: @"Close" rightTitle: @"More" leftBack: YES];
		[bottomNav pushNavigationItem: navItem];
		[bottomNav setDelegate: self];
		
		[fractalFrame addSubview: fractalView];
		[fractalFrame addSubview: optionsTable];
		[fractalFrame addSubview: bottomNav];
		
		[optionsTable reloadData];
	}
	return fractalFrame;
}

@end